<?php
namespace module\tv\controller;

use think\Request;
use core\tv\model\VideoModel;
use core\tv\logic\VideoLogic;
use core\tv\validate\VideoValidate;

class Video extends Base
{
    /**
     * 视频列表
     * 
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request)
    {
        $this->siteTitle = '视频管理';

        $map = [];

        // 查询条件-状态
        $status = $request->param('status', '');
        if ($status != '') {
            $status = intval($status);
            $map['status'] = $status;
        }
        $this->assign('status', $status);

        // 查询条件-视频类型
        $video_type = $request->param('video_type', '');
        if ($video_type != '') {
            $video_type = intval($video_type);
            $map['video_type'] = $video_type;
        }
        $this->assign('video_type', $video_type);

        // 查询条件-播放方式
        $play_type = $request->param('play_type', '');
        if ($play_type != '') {
            $play_type = intval($play_type);
            $map['play_type'] = $play_type;
        }
        $this->assign('play_type', $play_type);

        // 查询条件-关键词
        $keyword = $request->param('keyword');
        if ($keyword != '') {
            $map['video_name|video_title'] = [
                'like',
                '%' . $keyword . '%'
            ];
        }
        $this->assign('keyword', $keyword);
        
        //列表
        $model = VideoModel::getInstance()->where($map)->order('sort DESC');
        $this->_page($model);

        //视频选择项
        $this->getVideoSelect();

        return $this->fetch();
    }

    /**
     * 新增视频
     * 
     * @param Request $request
     * @return mixed
     */
    public function add(Request $request)
    {
        if($request->isPost()){
            $data = [
                'video_name' => $request->param('video_name'),
                'video_title' => $request->param('video_title'),
                'video_desc' => $request->param('video_desc'),
                'tag' => $request->param('tag'),
                'video_type' => $request->param('video_type'),
                'play_type' => $request->param('play_type'),
                'cate_id' => $request->param('cate_id'),
                'cover' => $request->param('cover'),
                'preview' => $request->param('preview'),
                'teacher' => $request->param('teacher'),
                'count_chapter' => $request->param('count_chapter'),
                'sort' => $request->param('sort'),
                'status' => $request->param('status'),
            ];

            //验证
            $this->_validate(VideoValidate::class, $data, 'add');

            //添加
            $this->_add(VideoModel::class, $data);
        }else{
            $this->siteTitle = '新增视频';

            //视频选择项
            $this->getVideoSelect();

            return $this->fetch();
        }
    }

    /**
     * 编辑视频
     * 
     * @param Request $request
     * @return mixed
     */
    public function edit(Request $request)
    {
        if($request->isPost()){
            $data = [
                'video_name' => $request->param('video_name'),
                'video_title' => $request->param('video_title'),
                'video_desc' => $request->param('video_desc'),
                'tag' => $request->param('tag'),
                'video_type' => $request->param('video_type'),
                'play_type' => $request->param('play_type'),
                'cate_id' => $request->param('cate_id'),
                'cover' => $request->param('cover'),
                'preview' => $request->param('preview'),
                'teacher' => $request->param('teacher'),
                'count_chapter' => $request->param('count_chapter'),
                'sort' => $request->param('sort'),
                'status' => $request->param('status'),
            ];

            //验证
            $this->_validate(VideoValidate::class, $data, 'edit');

            //添加
            $this->_edit(VideoModel::class, $data);
        }else{
            $this->siteTitle = '编辑视频';

            //记录
            $this->_record(VideoModel::class);

            //视频选择项
            $this->getVideoSelect();

            return $this->fetch();
        }
    }

    /**
     * 修改字段值
     *
     * @param Request $request            
     * @return mixed
     */
    public function modify(Request $request)
    {
        $fields = [
            'sort',
            'status',
            'video_type',
            'play_type',
            'cate_id',
            'count_chapter'
        ];
        $this->_modify(VideoModel::class, $fields);
    }

    /**
     * 删除视频
     * 
     * @param Request $request
     * @return void
     */
    public function delete(Request $request)
    {
        $this->_delete(VideoModel::class, false);
    }

    /**
     * 获取视频选择项
     * 
     * @return mixed
     */
    private function getVideoSelect()
    {
        //状态选择
        $videoStatus = VideoLogic::getInstance()->getVideoStatus();
        $this->assign('videoStatus',$videoStatus);

        //类型选择
        $videoType = VideoLogic::getInstance()->getVideoType();
        $this->assign('videoType',$videoType);

        //方式选择
        $playType = VideoLogic::getInstance()->getPlayType();
        $this->assign('playType',$playType);

        //分类选择
        $videoCate = VideoLogic::getInstance()->getVideoCate();
        $this->assign('videoCate',$videoCate);
    }
    
}